﻿using Lightray.Hattrick.ChppLib.EntityTypes;

namespace Lightray.Hattrick.ChppLib
{
    internal class AbilityVector
    {
        public enum VectorIndex
        {
            Playmaking = 0,
            Winger = 1,
            Scorer = 2,
            Keeper = 3,
            Passing = 4,
            Defending = 5,
            SetPieces = 6,
            Experience = 7,
            Leadership = 8
        }

        private Player _targetPlayer;
        private Weather _affectingWeather;
        private double _playmaking, _basePlaymaking;
        private double _scoring, _baseScoring;
        private double _defending, _baseDefending;
        private int _stamina, _baseStamina;        

        public Weather AffectingWeather
        {
            get { return _affectingWeather; }
            set
            {
                _affectingWeather = value;
                affectWeatherContribution();
            }
        }

        public int Form { get { return Player.GetSkillValue(_targetPlayer.Form); } }
        public int Experience { get { return Player.GetSkillValue(_targetPlayer.Experience); } }
        public int Stamina { get { return _stamina; } }

        public AbilityVector(Player target)
        {
            _targetPlayer = target;
            _basePlaymaking = Player.GetSkillValue(target.Playmaking);
            _baseDefending = Player.GetSkillValue(target.Defending);
            _baseScoring = Player.GetSkillValue(target.Scoring);
            _baseStamina = Player.GetSkillValue(target.Stamina);
            _affectingWeather = Weather.Unknown;
        }

        public double[] ToArray()
        {
            return new double[] 
            { 
                _playmaking,
                Player.GetSkillValue(_targetPlayer.Winger),
                _scoring,
                Player.GetSkillValue(_targetPlayer.Keeper),
                Player.GetSkillValue(_targetPlayer.Passing),
                _defending,
                Player.GetSkillValue(_targetPlayer.SetPieces),
                Player.GetSkillValue(_targetPlayer.Experience),
                Player.GetSkillValue(_targetPlayer.Leadership) 
            };
        }

        private void affectWeatherContribution()
        {
            _scoring = _baseScoring;
            _playmaking = _basePlaymaking;
            _defending = _baseDefending;
            _stamina = _baseStamina;

            switch (_affectingWeather)
            {
                case Weather.Rainy:
                    affectRainyWeather();
                    break;

                case Weather.Sunny:
                    affectSunnyWeather();
                    break;
            }
        }

        private void affectSunnyWeather()
        {
            switch (_targetPlayer.Specialty)
            {
                case Specialty.Technical:
                    _scoring *= 1.05;
                    _playmaking *= 1.05;
                    break;
                case Specialty.Quick:
                    _defending *= 0.95;
                    break;
                case Specialty.Powerful:
                    _scoring *= 0.95;
                    if (_baseStamina != 0)
                        _stamina--;
                    break;
            }
        }

        private void affectRainyWeather()
        {
            switch (_targetPlayer.Specialty)
            {
                case Specialty.Technical:
                    _scoring *= 0.95;
                    _playmaking *= 0.95;
                    break;

                case Specialty.Quick:
                    _scoring *= 0.95;
                    _defending *= 0.95;
                    break;

                case Specialty.Powerful:
                    _scoring *= 1.05;
                    _defending *= 1.05;
                    _playmaking *= 1.05;
                    break;
            }
        }
    }
}